#include<bits/stdc++.h>
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#include <immintrin.h>
//#include <emmintrin.h>
#define ll long long
#define ls(x) ((x)*2)
#define rs(x) ((x)*2+1)
#define fi first
#define se second
#define pii pair<int,int>
#define Debug(...) fprintf(stderr, __VA_ARGS__)
#define For(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define Rof(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
#define rep(i,  b) for(int i=1,i##end=b;i<=i##end;i++)
using namespace std;
const int N=1e6+5,base=999983,Mod=998244353;
const int base2=1000033,Mod2=1e9+7;
char buf[(1<<21)+5],*p1,*p2;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline void chmx(int &x,int y){(x<y)&&(x=y);}
inline void chmn(int &x,int y){(x>y)&&(x=y);}
inline void Add(int &x,int y){(x=x+y+Mod)%=Mod;}
inline int read(){
	int f=0,x=0;
	char ch=getchar();
	while(!isdigit(ch)){f|=(ch=='-');ch=getchar();}
	while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return f?-x:x;
}
void print(int n){
    if(n<0){
        putchar('-');
        n*=-1;
    }
    if(n>9) print(n/10);
    putchar(n%10+'0');
}

int n;
pii a[N]; 
inline ll work(int x){
	return 1ll*(x+1)*x/2;
}
int dis[4005][4005];
unordered_map<int,int>id[N];
int fx[4]={0,1,0,-1};
int fy[4]={1,0,-1,0};
pii gg[N*16]; 
int p[N][4];
ll prel[N],sufl[N];
ll prer[N],sufr[N];
ll mpl[N],mpr[N];
ll ans=0,f[N],RT;
int dep[N];
vector<int>q[N];
int siz[N];
void dfs(int x,int faz){
	dep[x]=dep[faz]+1;
	RT+=dep[x]-1;siz[x]=1;
	for(auto y:q[x]){
		if(y==faz)continue;
		dfs(y,x);siz[x]+=siz[y];
	}
}void dfs2(int x,int faz,ll z){
	ans+=z;
	for(auto y:q[x]){
		if(y==faz)continue;
		dfs2(y,x,z-siz[y]+n-siz[y]);
	}
}
signed main(){
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	// ios::sync_with_stdio(false);
	// cin.tie(0); cout.tie(0);
	n=read();
	int Lx=1e9,Rx=0;
	int Ly=1e9,Ry=0;
	For(i,1,n){
		a[i]={read(),read()};
		id[a[i].fi][a[i].se]=i;
		Lx=min(Lx,a[i].fi);
		Rx=max(Rx,a[i].fi);
		Ly=min(Ly,a[i].se);
		Ry=max(Ry,a[i].se);
	}
	if(1ll*(Rx-Lx+1)*(Ry-Ly+1)==n){
		int a=Rx-Lx+1,b=Ry-Ly+1;
		For(i,1,a){
			For(j,1,b){
				ans+=1ll*work(i-1)*b;
				ans+=1ll*work(a-i)*b;
				ans+=1ll*work(b-j)*a;
				ans+=1ll*work(j-1)*a;
				
			}
		}
		printf("%lld\n",ans/2);
		return 0;
	}
	For(i,1,n){
		For(j,0,3){
			int px=a[i].fi+fx[j],py=a[i].se+fy[j];
			if(px<0) continue;
			if(!id[px].count(py)) continue;
			p[i][j]=id[px][py];
			
		}
	}
	if(n<=4000){
		memset(dis,0x3f,sizeof(dis));
		int mx=dis[0][0]; 
		int L=1,R=0;
		For(i,1,n) dis[i][i]=0,gg[++R]={i,i};
		while(L<=R){
			int x=gg[L].fi,y=gg[L].se;L++;
			For(i,0,3){
				int ID=p[x][i];
				if(ID&&dis[ID][y]==mx){
					dis[ID][y]=dis[x][y]+1;
					gg[++R]={ID,y};
				}
			}
			For(i,0,3){
				int ID=p[x][i];
				if(ID&&dis[x][ID]==mx){
					dis[x][ID]=dis[x][y]+1;
					gg[++R]={x,ID};
				}
			}
		}
		For(i,1,n)For(j,i+1,n)ans+=dis[i][j];
		printf("%lld\n",ans);
		return 0;
	}
	int sss=0;
	For(i,1,n)For(j,0,3)if(p[i][j])sss++;
	sss/=2;
	if(sss==n-1){
		For(i,1,n){
			For(j,0,3){
				if(p[i][j]){
					q[i].push_back(p[i][j]);
				}
			}
		}
		dfs(1,0);
		dfs2(1,0,RT);
		printf("%lld\n",ans/2);
		return 0;
	} 
		sort(a+1,a+n+1);
		int L=Lx,R=Ly;
		For(i,1,n){
			a[i].fi-=L;
			a[i].se-=R;
			mpl[a[i].fi]++;
			mpr[a[i].se]++; 
		}
		ll ss=0,sum=0;
		For(i,0,n){
			prel[i]=ss;
			sum+=mpl[i];
			ss+=sum;
		}
			ss=0,sum=0;
		Rof(i,n,0){
			sufl[i]=ss;
			sum+=mpl[i];
			ss+=sum;
		}ss=0,sum=0;
		For(i,0,n){
			prer[i]=ss;
			sum+=mpr[i];
			ss+=sum;
		}
		ss=0,sum=0;
		Rof(i,n,0){
			sufr[i]=ss;
			sum+=mpr[i];
			ss+=sum;
		}
		Rof(i,n,1){
			ans+=prel[a[i].fi]+prer[a[i].se];
			ans+=sufl[a[i].fi]+sufr[a[i].se];
		}printf("%lld\n",ans/2);
#ifdef LOCAL
    Debug("\nMy Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}

